﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PlatWL.Data;

namespace PlatWL.Services.Services
{
    public class AdvertManage
    {
        public void GetList(ref Msg_Result msg, int Client, Msg_Page page,string searchkey,string type1,string isshelf,string client)
        {
            StringBuilder where = new StringBuilder();
            where.Append("1=1");

            if (client != "")
            {
                where.AppendFormat(" and h.Client={0}", client);
            }
            //if (type2 != "")
            //{
            //    where.AppendFormat(" and h.Type2='{0}'", type2);
            //}
            //if (isself != "")
            //{
            //    where.AppendFormat(" and h.IsShelf='{0}'", isself);
            //}
            if (searchkey != "")
            {
                where.AppendFormat(" and h.Title like '%{0}%'", searchkey);
            }

            int total = 0;
            var dt = new pt_info_advertb().GetDataPager("pt_info_advert h join pt_base_client c on h.Client=c.ID", "h.*,c.CodeName", page.pagesize, page.pageindex, "h.CRTime desc", where.ToString(), ref total);

            msg.total = total;
            msg.result = dt;


        }

        public void GetModel(ref Msg_Result msg,int Client, int ID)
        {
            new pt_info_advertb().ExsSql("update PT_Info_Advert set ViewQty=IFNULL(ViewQty,0)+1 where ID=" + ID);
            msg.result = new pt_info_advertb().GetEntity(d => d.ID == ID);
        }

        public void DelModel(ref Msg_Result msg, int Client, int ID)
        {
            if (Client == 0)
            {
                new pt_info_advertb().Delete(d => d.ID == ID);
            }
            else
            {
                new pt_info_advertb().Delete(d => d.Client == Client && d.ID == ID);
            }
        }
        public void AddModel(ref Msg_Result msg, int Client, pt_info_advert adv,pt_auth_user auth_user)
        {
            bool result = false;
            if (adv.ID == 0)
            {
                //adv.Client = Client;
                adv.CRUser = auth_user.ID;
                adv.CRTime = DateTime.Now;
                result = new pt_info_advertb().Insert(adv);
            }
            else
            {
                result = new pt_info_advertb().Update(adv);
            }
            if (!result)
            {
                msg.error = "更新失败";
            }
        }

        //设置上下架
        public void SetShelf(ref Msg_Result msg, int Client, int ID)
        {
            new pt_info_advertb().ExsSql("UPDATE pt_info_advert set IsShelf=case when IsShelf=1 then 0 else 1 END WHERE ID=" + ID);
        }
    }
}
